"""
    Author:
    Date: 2019-04-03

    微信管理后台-市场营销
"""
import datetime

from django.shortcuts import render

from entities import app_mgr
from entities import user_mgr
from entities.consts import *
from utils import util

from .. import helpers
from .. helpers import gma_login_required


@gma_login_required
def channel_list(request):
    """ 渠道列表 """
    begin_time, end_time = util.get_today_interval()

    data = []
    apps = ['quyue', 'yuexia', 'langhua', 'yuejian', 'yuanquan', 'kuaimao']
    for app in apps:
        app_info = get_app_info(app)
        channels = app_mgr.get_app_channels_basic_info(begin_time, end_time, app)
        data.append((app_info, channels))

    # context
    context = helpers.build_context(request)
    context['data'] = data
    return render(request, 'gma/market/channels_list.html', context)


@gma_login_required
def channel_home(request, channel, app):
    """ 渠道首页 """
    begin_time = '2018-01-01 00:00:00'
    end_time = util.datetimetostr(datetime.datetime.now())

    channel_create_time = user_mgr.get_channel_create_time(channel)
    total_caster_count = user_mgr.get_new_caster_count(begin_time, end_time, channel)
    total_user_count = user_mgr.get_new_user_count(begin_time, end_time, channel, request=request) - total_caster_count
    paid_user_count = user_mgr.get_paid_new_user_count(begin_time, end_time, channel)
    repaid_user_count = user_mgr.get_repaid_new_user_count(begin_time, end_time, channel)
    paid_amount = user_mgr.get_new_user_pay_amount(begin_time, end_time, channel)
    rpur = '%.2f' % ((100*repaid_user_count/paid_user_count) if paid_user_count > 0 else 0)
    pur = '%.2f' % ((100*paid_user_count/total_user_count) if total_user_count > 0 else 0)
    arpu = '%.2f' % ((paid_amount/total_user_count) if total_user_count > 0 else 0)
    arppu = '%.2f' % (int(paid_amount / paid_user_count) if paid_user_count > 0 else 0)

    paid_amount_30 = user_mgr.get_new_user_pay_amount(begin_time, end_time, channel, None, 30)
    paid_amount_98 = user_mgr.get_new_user_pay_amount(begin_time, end_time, channel, None, 98)
    paid_amount_298 = user_mgr.get_new_user_pay_amount(begin_time, end_time, channel, None, 298)
    paid_amount_588 = user_mgr.get_new_user_pay_amount(begin_time, end_time, channel, None, 588)
    paid_amount_1388 = user_mgr.get_new_user_pay_amount(begin_time, end_time, channel, None, 1388)
    pr30 = '%.2f' % (int(100*paid_amount_30 / paid_amount) if paid_amount > 0 else 0)
    pr98 = '%.2f' % (int(100*paid_amount_98 / paid_amount) if paid_amount > 0 else 0)
    pr298 = '%.2f' % (int(100*paid_amount_298 / paid_amount) if paid_amount > 0 else 0)
    pr588 = '%.2f' % (int(100*paid_amount_588 / paid_amount) if paid_amount > 0 else 0)
    pr1388 = '%.2f' % (int(100*paid_amount_1388 / paid_amount) if paid_amount > 0 else 0)

    # context
    context = helpers.build_context(request)
    context['app'] = get_app_info(app)
    context['channel'] = get_channel_info(channel)
    context['create_time'] = channel_create_time
    context['total_caster_count'] = total_caster_count
    context['total_user_count'] = total_user_count
    context['paid_user_count'] = paid_user_count
    context['repaid_user_count'] = repaid_user_count
    context['paid_amount'] = paid_amount
    context['rpur'] = rpur
    context['pur'] = pur
    context['arpu'] = arpu
    context['arppu'] = arppu

    context['pr30'] = pr30
    context['pr98'] = pr98
    context['pr298'] = pr298
    context['pr588'] = pr588
    context['pr1388'] = pr1388
    return render(request, 'gma/market/channels_home.html', context)


@gma_login_required
def index(request):
    """ 首页 """
    begin_time, end_time = util.get_today_interval()

    # 每日新增
    new_users = []
    records = user_mgr.get_new_users_by_channel(begin_time, end_time)
    for record in records:
        channel = record['channel']
        value = record['value']
        new_users.append({'channel': channel, 'value': value})
    new_users.sort(key=lambda x: x['value'], reverse=True)

    # 每日充值
    pay_data = []
    records = user_mgr.get_pay_data_by_channel(begin_time, end_time)
    for record in records:
        channel = record['channel']
        value = record['value']
        pay_data.append({'channel': channel, 'value': value})
    pay_data.sort(key=lambda x: x['value'], reverse=True)

    # context
    context = helpers.build_context(request)
    context['apps'] = ['YYZB']
    context['new_users'] = new_users
    context['pay_data'] = pay_data
    return render(request, 'gma/market/market_home.html', context)


@gma_login_required
def new_user_chart_in_channel_detail(request, channel):
    """ 渠道新增用户的统计详情 """
    records = user_mgr.get_new_users_archive_data_in_channel(channel, 50)
    data = []
    for r in records:
        date = util.strtodatetime(r['user_create_date'])
        value = r['user_count']
        unit = '人'
        data.append((date, value, unit))

    context = helpers.build_context(request)
    context['channel'] = channel
    context['data'] = data
    context['data_label'] = f'新增用户'
    context['data_title'] = f'{channel}每日新增用户趋势图'
    return render(request, 'gma/market/chart_detail_new_user.html', context)


@gma_login_required
def pay_chart_in_channel_detail(request, channel):
    """ 渠道充值的统计详情 """
    records = user_mgr.get_pay_archive_data_in_channel(channel, 50)
    data = []
    for r in records:
        date = util.strtodatetime(r['pay_date'])
        value = r['pay_money']
        unit = '元'
        data.append((date, value, unit))

    context = helpers.build_context(request)
    context['data'] = data
    context['channel'] = channel
    context['data_label'] = f'新增用户'
    context['data_title'] = f'{channel}每日新增用户趋势图'
    return render(request, 'gma/market/chart_detail_pay.html', context)


@gma_login_required
def channel_daily_brief(request, channel, day):
    """ 指定时间范围内的充值列表 """
    begin_time = day
    end_time = day + ' 23:59:59'

    paid_new_user_count = user_mgr.get_paid_new_user_count(begin_time, end_time, channel)
    new_caster_count = user_mgr.get_new_caster_count(begin_time, end_time, channel)
    new_user_count = user_mgr.get_new_user_count(begin_time, end_time, channel, request=request) - new_caster_count
    paid_amount = user_mgr.get_new_user_pay_amount(begin_time, end_time, channel)

    context = helpers.build_context(request)
    context['channel'] = channel
    context['day'] = day

    # 近期新增用户
    context['new_user_count'] = new_user_count
    # 近期新增主播
    context['new_caster_count'] = new_caster_count
    # 近期充值新增用户
    context['paid_new_user_count'] = paid_new_user_count
    # PUR
    context['pur'] = '%.2f' % ((100*paid_new_user_count/new_user_count) if new_user_count > 0 else 0)
    # 近期充值金额
    context['paid_amount'] = paid_amount
    # ARPU
    context['arpu'] = '%.2f' % ((paid_amount/new_user_count) if new_user_count > 0 else 0)
    # ARPPU
    context['arppu'] = '%.2f' % (int(paid_amount/paid_new_user_count) if paid_new_user_count > 0 else 0)
    return render(request, 'gma/market/channel_daily_brief.html', context)


if __name__ == '__main__':
    print('ok')
